package net.xju.controller;

import net.xju.common.framework.simplemvc.RequestMapping;
import net.xju.common.utils.JSONUtil;
import net.xju.common.utils.PageInfo;
import net.xju.common.utils.Result;
import net.xju.entity.User;
import net.xju.service.UserService;
import net.xju.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/api/user")
public class UserController {
    UserService userService = new UserServiceImpl();

//    获取登录用户
    @RequestMapping("/userInfo.do")
    public void userInfo(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        if (user == null){
            JSONUtil.printByJSON(resp, Result.fail("请登录"));
        }else{
            JSONUtil.printByJSON(resp, Result.success(user, "查询成功"));
        }
    }

//车辆租赁登录
    @RequestMapping("/login.do")
    public void login(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String role = req.getParameter("role");
        System.out.println(username);
        System.out.println(password);
        System.out.println(role);
        User user = userService.login(username, password, role);
        System.out.println(user);
        if(user != null){
            HttpSession session = req.getSession();
            session.setAttribute("user", user);
            JSONUtil.printByJSON(resp, Result.success(user,"登录成功"));
        }else{
            JSONUtil.printByJSON(resp, Result.fail("用户名或密码错误"));
        }
    }


//    登录
//    @RequestMapping("/login.do")
//    public  void login(HttpServletRequest req, HttpServletResponse resp) {
//
//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
//        User user = userService.login(username, password);
//        if(user != null){
//            HttpSession session = req.getSession();
//            session.setAttribute("user", user);
//            JSONUtil.printByJSON(resp, Result.success(user,"登录成功"));
//        }else{
//            JSONUtil.printByJSON(resp, Result.fail("用户名或密码错误"));
//        }
//    }

//    新增数据
    @RequestMapping("/insert.do")
    public  void insert(HttpServletRequest req, HttpServletResponse resp) {

            User  user = new User();
        try {
            req.setCharacterEncoding("utf-8");
        } catch (java.io.UnsupportedEncodingException e) {
            JSONUtil.printByJSON(resp, Result.fail("字符编码设置失败"));
            return;
        }

        user.setRole(Integer.parseInt(req.getParameter("role")));
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.setEmail(req.getParameter("email"));
        user.setGender(req.getParameter("gender"));
        user.setAvatar(req.getParameter("avatar"));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userService.insert(user);
        JSONUtil.printByJSON(resp, Result.success(user, "添加成功"));
    }

//    根据id查询
//    @RequestMapping("/selectOne.do")
//    public void selectOne(HttpServletRequest req, HttpServletResponse resp) {
//        int id = Integer.parseInt(req.getParameter("id"));
//        User user = userService.selectOne(id);
//        if (user == null){
//            JSONUtil.printByJSON(resp, Result.fail("用户不存在"));
//        }else {
//            JSONUtil.printByJSON(resp, Result.success(user,"查询成功"));}
//    }

//    根据id删除
    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) {
        int id = Integer.parseInt(req.getParameter("id"));
        System.out.println(id);
        userService.delete(id);
        JSONUtil.printByJSON(resp, Result.success("删除成功"));
    }
//    修改数据
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) {
        User user = new User();
        try {
            req.setCharacterEncoding("utf-8");
        } catch (java.io.UnsupportedEncodingException e) {
            JSONUtil.printByJSON(resp, Result.fail("字符编码设置失败"));
            return;
        }
        user.setId(Integer.parseInt(req.getParameter("id")));
//        user.setRole(Integer.parseInt(req.getParameter("role")));
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.setEmail(req.getParameter("email"));
        user.setGender(req.getParameter("gender"));
        user.setAvatar(req.getParameter("avatar"));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userService.update(user);
        JSONUtil.printByJSON(resp, Result.success(user, "修改成功"));
    }

//    分页查找
    @RequestMapping("/list.do")
    public void pagelist(HttpServletRequest req, HttpServletResponse resp) {

        String username = req.getParameter("username");
        String gender = req.getParameter("gender");

        String pageNumStr = req.getParameter("pageNum");
        int pageNum = pageNumStr == null ? 1 : Integer.parseInt(pageNumStr);

        String pageSizeStr = req.getParameter("pageSize");
        int pageSize = pageSizeStr == null ? 10 : Integer.parseInt(pageSizeStr);

        PageInfo<User> pageInfo = userService.pageList(username, gender, pageNum, pageSize);
        if (pageInfo == null){
            JSONUtil.printByJSON(resp, Result.fail("没有数据"));
        }else{
            System.out.println(pageInfo.getRecords());
            JSONUtil.printByJSON(resp, Result.success(pageInfo, "查询成功"));
        }
    }

    }

